package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.WebUtils;
import com.xjh.t.domain.Permission;
import com.xjh.t.domain.Saler;
import com.xjh.t.domain.User;
import com.xjh.t.service.ISalerService;
import com.xjh.t.vo.PermissionVo;
import com.xjh.t.vo.SalerVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2025-11-09
 */
@RestController
@RequestMapping("/saler")
public class SalerController {

    @Autowired
    private ISalerService salerService;

    @RequestMapping("loadAllSaler")
    public DataGridView loadAllSaler(SalerVo vo) {
        QueryWrapper<Saler> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vo.getSalerName()), "saler_name", vo.getSalerName());
        queryWrapper.like(StringUtils.isNotBlank(vo.getMobilePhone()), "mobile_phone", vo.getMobilePhone());
        IPage<Saler> page = new Page<>(vo.getPage(), vo.getLimit());

        this.salerService.page(page, queryWrapper);

        return new DataGridView(page.getTotal(), page.getRecords());

    }
    @RequestMapping("loadAllValidSaler")
    public List<Saler> loadAllValidSaler(SalerVo vo) {
        QueryWrapper<Saler> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("valid", 1);


        return this.salerService.list(queryWrapper);

    }


    @RequestMapping("addSaler")
    public ResultObj addSaler(Saler vo) {
        QueryWrapper<Saler> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(vo.getMobilePhone()), "mobile_phone", vo.getMobilePhone());

        List<Saler> list = this.salerService.list(queryWrapper);
        if (list.size() > 0)
        {
            return ResultObj.ADD_SALER_FAIL;
        }
        vo.setCreateTime(new Date());

        salerService.save(vo);
        return ResultObj.ADD_SUCESS;

    }

    @RequestMapping("updateSaler")
    public ResultObj updateSaler(Saler vo) {

        salerService.updateById(vo);
        return ResultObj.UPDATE_SUCESS;

    }

    @RequestMapping("deletetSaler")
    public ResultObj deletetSaler(int id) {

        salerService.removeById(id);
        return ResultObj.DELETE_SUCESS;

    }
}

